﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace MyTest
{
    /// <summary>
    /// 教员:薛海
    /// 微信:xuehai03
    /// QQ:289477003
    /// 简易播放器  支持下一首
    /// 2019-2-20
    /// </summary>
    public partial class frmMus : Form
    {
        public frmMus()
        {
            InitializeComponent();
        }

        List<string> listSong = new List<string>();


        //播放
        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (this.btnPlay.Text == "播放")
            {
                this.axWindowsMediaPlayer1.URL = listSong[listBox1.SelectedIndex];
                this.axWindowsMediaPlayer1.Ctlcontrols.play();
                this.btnPlay.Text = "暂停";
            }
            else
            {
                this.axWindowsMediaPlayer1.Ctlcontrols.pause();
                this.btnPlay.Text = "播放";
            }
        }

        //上一曲
        private void btnUp_Click(object sender, EventArgs e)
        {
            // 获得当前选中歌曲的索引
            int index = listBox1.SelectedIndex;
            index--;
            if (index < 0)
            {
                index = listBox1.Items.Count - 1;
            }
            //将重新改变后的索引重新的赋值给当前选中项
            listBox1.SelectedIndex = index;
            axWindowsMediaPlayer1.URL = listSong[index];
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }
        //下一曲
        private void btnNext_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            index++;
            if (index == listBox1.Items.Count)
            {
                index = 0;
            }
            listBox1.SelectedIndex = index;
            axWindowsMediaPlayer1.URL = listSong[index];
            axWindowsMediaPlayer1.Ctlcontrols.play();

        }

        //选择歌曲
        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择";
            ofd.Multiselect = true;
            ofd.Filter = "音乐文件|*.mp3|*.wav|mp3文件|所有文件|*.*";

            this.textBox1.Text = ofd.SafeFileName;

            ofd.ShowDialog();

            //获得我们在文件夹中选择所有文件的全路径
            string[] path = ofd.FileNames;
            for (int i = 0; i < path.Length; i++)
            {
                //将音乐文件的文件名加载到ListBox中
                listBox1.Items.Add(Path.GetFileName(path[i]));
                //将音乐文件的全路径存储到泛型集合中
                listSong.Add(path[i]);
            }
        }

        //录音模块
        [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
        public static extern int mciSendString(
         string lpstrCommand,
         string lpstrReturnString,
         int uReturnLength,
         int hwndCallback
        );

        //开始录音
        private void button1_Click(object sender, EventArgs e)
        {
            mciSendString("set wave bitpersample 8", "", 0, 0);

            mciSendString("set wave samplespersec 20000", "", 0, 0);
            mciSendString("set wave channels 2", "", 0, 0);
            mciSendString("set wave format tag pcm", "", 0, 0);
            mciSendString("open new type WAVEAudio alias movie", "", 0, 0);

            mciSendString("record movie", "", 0, 0);

        }

        //停止录音
        private void button2_Click(object sender, EventArgs e)
        {
            mciSendString("stop movie", "", 0, 0);
            mciSendString("save movie 1.wav", "", 0, 0);
            mciSendString("close movie", "", 0, 0);

        }

        private void frmMus_Load(object sender, EventArgs e)
        {
           // getInfo();
        }


    }
}
